package cn.holy.app.xcddt.ui.activity.manager

import androidx.lifecycle.Observer
import com.google.android.material.tabs.TabLayout
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import androidx.core.content.ContextCompat
import androidx.viewpager.widget.ViewPager
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import android.text.format.DateUtils
import android.view.View
import android.widget.Button
import android.widget.TextView
import cn.holy.app.xcddt.R
import cn.holy.app.xcddt.app.App
import cn.holy.app.xcddt.domain.RecordBean
import cn.holy.app.xcddt.domain.RecordGroup
import cn.holy.app.xcddt.repository.RecordRepository

import com.alibaba.android.arouter.facade.annotation.Route
import com.qmuiteam.qmui.util.QMUIStatusBarHelper
import com.qmuiteam.qmui.widget.QMUITopBar

import java.util.ArrayList


import cn.holy.app.xcddt.ui.activity.base.BaseActivity
import cn.holy.app.xcddt.ui.fragment.LocalRecordFragment
import cn.holy.app.xcddt.utils.HDateUtils
import com.alibaba.android.arouter.launcher.ARouter
import com.orhanobut.logger.Logger
import com.qmuiteam.qmui.widget.dialog.QMUIDialog
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction
import com.zhy.adapter.recyclerview.CommonAdapter
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter
import com.zhy.adapter.recyclerview.base.ViewHolder
import kotlinx.android.synthetic.main.activity_manager.*
import javax.inject.Inject

/**
 * Created by 69568 on 2018/2/24.
 */

@Route(path = "/app/manager")
class ManagerActivity : BaseActivity() {


    @Inject
    lateinit var recordRepository: RecordRepository

    var groups: ArrayList<RecordGroup> = ArrayList()
    lateinit var commonAdapter: CommonAdapter<RecordGroup>

    override fun initData() {


        recordRepository.recordRecordGroup?.observe(this, Observer<List<RecordGroup>> { t ->
            groups.clear()
            if (t != null) {
                groups.addAll(t)
            }
            Logger.d(groups)
            commonAdapter.notifyDataSetChanged()
        })
    }

    override fun initView() {
        App.appComponent.inject(this)
        QMUIStatusBarHelper.translucent(this)
        QMUIStatusBarHelper.setStatusBarDarkMode(this)
        top_bar.setTitle("暂存记录").setTextColor(ContextCompat.getColor(this, R.color.colorWhite))
        top_bar.addLeftImageButton(R.mipmap.ic_back, R.id.action_close).setOnClickListener({ finish() })
        val linearLayoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
        recyclerView.layoutManager = linearLayoutManager
        recyclerView.addItemDecoration(DividerItemDecoration(this, DividerItemDecoration.VERTICAL))

        commonAdapter = object : CommonAdapter<RecordGroup>(this, R.layout.layout_record_group_item, groups) {
            override fun convert(holder: ViewHolder, recordGroup: RecordGroup, position: Int) {
                holder.getView<TextView>(R.id.tv_group).text = "第" + recordGroup.record_num + "次巡查"
                holder.getView<TextView>(R.id.tv_group_time).text = HDateUtils.dateToStr(recordGroup.record_date!!)
                holder.getView<TextView>(R.id.tv_record_username).text = recordGroup.record_username
                holder.getView<TextView>(R.id.tv_record_people).text = recordGroup.record_people
                val button = holder.getView<Button>(R.id.btn_end_group)
                button.setOnClickListener {
                    QMUIDialog.MessageDialogBuilder(this@ManagerActivity)
                            .setTitle("提示")
                            .setMessage("确定要结束这次巡查吗？")
                            .addAction(QMUIDialogAction(this@ManagerActivity,"确定",QMUIDialogAction.ActionListener { dialog, index ->
                                dialog.dismiss()
                                recordGroup.status = 1
                                recordRepository.saveRecordGroup(recordGroup)
                            }))
                            .addAction(QMUIDialogAction(this@ManagerActivity,"取消",QMUIDialogAction.ActionListener { dialog, index ->
                                dialog.dismiss()
                            }))
                            .show()
                }
                val view = holder.getView<TextView>(R.id.upload_status)
                if (recordGroup.status == 0) {
                    view.visibility = View.GONE
                    button.visibility = View.VISIBLE

                } else {
                    view.visibility = View.VISIBLE
                    button.visibility = View.GONE
                }
            }
        }
        commonAdapter.setOnItemClickListener(object : MultiItemTypeAdapter.OnItemClickListener {
            override fun onItemClick(view: View?, holder: RecyclerView.ViewHolder?, position: Int) {
                ARouter.getInstance().build("/app/manager/list").withInt("record_num", groups[position].record_num).navigation()
            }

            override fun onItemLongClick(view: View?, holder: RecyclerView.ViewHolder?, position: Int): Boolean {
                return true
            }

        })

        recyclerView.adapter = commonAdapter
    }

    override fun initLayout() {
        setContentView(R.layout.activity_manager)
    }


}
